package com.mvvm.ktnet.base.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProvider
import com.mvvm.ktnet.application.App
import com.mvvm.ktnet.base.IBaseView
import com.mvvm.ktnet.base.viewmodel.BaseViewModel
import com.mvvm.ktnet.retention.ViewModelProviders
import me.hgj.jetpackmvvm.ext.getVmClazz


abstract class IBaseVmFragment<VM : BaseViewModel> : Fragment(), IBaseView {

    //是否第一次加载
    private var isFirst: Boolean = true

    lateinit var mViewModel: VM

    lateinit var mActivity: AppCompatActivity

    /**
     * 当前Fragment绑定的视图布局
     */
    abstract fun layoutId(): Int

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(layoutId(), container, false)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isFirst = true
        mViewModel = createViewModel()
        ViewModelProviders<BaseViewModel>()
        initView(savedInstanceState)
        if (immersionBarEnabled()) initStatusBar()
        createObserver()
        registerUiChange()
        registorDefErrorChange()
        initLoad()
        initData()
    }

    override fun initStatusBar() {

    }

    override fun immersionBarEnabled(): Boolean = false


    /**
     * 创建viewModel
     */
    private fun createViewModel(): VM {
        return ViewModelProvider(
            this,
            SavedStateViewModelFactory(App.context, this)
        ).get(getVmClazz(this))
    }


    /**
     * 懒加载
     */
    abstract fun lazyLoadData()


    override fun onResume() {
        super.onResume()
        onVisible()
    }

    /**
     * 是否需要懒加载
     */
    private fun onVisible() {
        if (lifecycle.currentState == Lifecycle.State.STARTED && isFirst) {
            //延迟加载0.12秒加载 避免fragment跳转动画和渲染ui同时进行，出现些微的小卡顿
            view?.postDelayed({
                lazyLoadData()
                isFirst = false
            }, 120)
        }
    }


    /**
     * 注册 UI 事件
     */
    override fun registerUiChange() {
//        mViewModel.loadingChange.showDialog.observe(viewLifecycleOwner, Observer {
//            showLoading()
//        })

    }

    /**
     * 将非该Fragment绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
     * @param viewModels Array<out BaseViewModel>
     */
    override fun addLoadingObserve(vararg viewModels: BaseViewModel) {
//        viewModels.forEach { viewModel ->
//            //显示弹窗
//            viewModel.loadingChange.showDialog.observe(viewLifecycleOwner, Observer {
//                showLoading()
//            })
//        }
    }

}
